package studio.raptor.ddal.dashboard.log.queue.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.dashboard.log.queue.interfaces.IMessageQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class JavaMessageQueue implements IMessageQueue {

	private static Logger log = LoggerFactory.getLogger( JavaMessageQueue.class);

	//LinkedBlockingQueue的容量，在不指定时容量为Integer.MAX_VALUE；基于链表的队列，此队列按 FIFO（先进先出）排序元素
	private LinkedBlockingQueue queue=new LinkedBlockingQueue();

	public Object readObject() {
		Object object=null;
		try{
			//take 移除并返回队列头部的元素；如果队列为空，则阻塞
			object=queue.take();
			log.debug("读取到对象 object:"+object);
		}catch(Exception e){
			e.printStackTrace();
			log.error("取内存队列数据异常", e) ;
		}
		return object;
	}

	public void writeObject(Object object) {
		//add 增加一个元索；如果队列已满，则抛出一个IllegalStateException异常
		log.debug("队列增加对象 object:"+object);
		queue.add(object);
	}

	public int getLength(){
		return queue.size();
	}

	public LinkedBlockingQueue getQueue() {
		return queue;
	}

	public void setQueue(LinkedBlockingQueue queue) {
		this.queue = queue;
	}

}
